package com.shroo8.footballblog.bo.api;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.shroo8.footballblog.dto.api.AuthorDto;
import com.shroo8.footballbloglib.dao.footballblogread.GetUserIdDao;
import com.shroo8.footballbloglib.dao.footballblogwrite.CreateUserDao;

@Service(value="createAuthorBo")
public class CreateAuthorBoImpl implements CreateAuthorBo{
	
	@Resource(name="getUserIdDao")
	private GetUserIdDao getUserIdDao;

	@Resource(name="createUserDao")
	private CreateUserDao createUserDao;
	
	private static final Logger logger = LoggerFactory.getLogger(CreateAuthorBoImpl.class);

	@Override
	public String createAuthor(AuthorDto authorDto) {
		
		String authorId = getUserIdDao.execute(authorDto.getType(), authorDto.getExternalId());
		
		if(authorId == null || authorId.equals("")){
			//create the author
			logger.info("Did not find the user in the db, will need to create it");
			authorId = createUserDao.execute(authorDto);
			return authorId;
		}else{
			logger.info("found the user, the id is being returned");
			return authorId;
		}
	}

}
